package com.itbaizhan.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.*;
import com.itbaizhan.shopping_common.service.GoodsService;
import com.itbaizhan.shopping_common.service.SearchService;
import com.itbaizhan.shopping_goods_service.mapper.GoodsImageMapper;
import com.itbaizhan.shopping_goods_service.mapper.GoodsMapper;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.directory.SearchControls;
import java.util.ArrayList;
import java.util.List;

/**
 * @author spf
 * @date 2023/12/26
 * @time 14:20
 */

// 商品服务实现类
@Service
@Component
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImageMapper goodsImageMapper;
//    @Reference
//    private SearchService searchService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 新增商品
    @Override
    public void add(Goods goods) {
        // 新增商品
        goodsMapper.insert(goods);
        // 新增商品图片
        Long goodsId = goods.getId();
        List<GoodsImage> goodsImageList = goods.getImages();
        for (GoodsImage goodsImage : goodsImageList) {
            goodsImage.setGoodsId(goodsId);
            goodsImageMapper.insert(goodsImage);
        }
        // 获取商品规格
        List<Specification> specificationList = goods.getSpecifications();
        // 获取商品规格项
        ArrayList<SpecificationOption> options = new ArrayList<>();
        for (Specification specification : specificationList) {
            options.addAll(specification.getSpecificationOptions());
        }
        // 新增商品——规格项
        for (SpecificationOption option : options) {
            goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
        }

        // 将商品同步到ES中
        if (goods.getIsMarketable()) {
            GoodsDesc goodsDesc = findDesc(goodsId);
//            searchService.syncGoodsToES(goodsDesc);
            rabbitTemplate.convertAndSend("goods_exchange", "sync_goods", goodsDesc);
        }

    }

    // 更新商品
    @Override
    public void update(Goods goods) {
        // 获取商品id
        Long goodsId = goods.getId();
        // 删除旧的规格项
        goodsMapper.deleteGoodsSpecificationOption(goodsId);
        // 删除旧的商品图片
        goodsImageMapper.deleteById(goodsId);
        // 新增新的
        goodsMapper.updateById(goods);
        List<Specification> specificationList = goods.getSpecifications();
        List<SpecificationOption> options = new ArrayList();
        for (Specification specification : specificationList) {
            options.addAll(specification.getSpecificationOptions());
        }

        for (SpecificationOption option : options) {
            goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
        }

        // 将商品同步到ES中
        if (goods.getIsMarketable()) {
            GoodsDesc goodsDesc = findDesc(goodsId);
//            searchService.syncGoodsToES(goodsDesc);
            rabbitTemplate.convertAndSend("goods_exchange", "sync_goods", goodsDesc);
        } else {
//            searchService.delete(goodsId);
            rabbitTemplate.convertAndSend("goods_exchange", "del_goods", goodsId);
        }

        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoodId(goods.getId());
        cartGoods.setGoodsName(goods.getGoodsName());
        cartGoods.setPrice(goods.getPrice());
        cartGoods.setHeaderPic(goods.getHeaderPic());
        rabbitTemplate.convertAndSend("goods_exchange", "sync_cart", cartGoods);
    }

    // 商品上架下架
    @Override
    public void putAway(Long id, Boolean isMarketable) {
        goodsMapper.putAway(id, isMarketable);
        // 将商品同步到ES中
        if (isMarketable) {
            GoodsDesc goodsDesc = findDesc(id);
//            searchService.syncGoodsToES(goodsDesc);
            rabbitTemplate.convertAndSend("goods_exchange", "sync_goods", goodsDesc);

        } else {
//            searchService.delete(id);
            rabbitTemplate.convertAndSend("goods_exchange", "del_goods", id);
            // 修改后将商品信息同步到redis购物车中
            CartGoods cartGoods = new CartGoods();
            cartGoods.getGoodId();
            rabbitTemplate.convertAndSend("goods_exchange", "del_cart", cartGoods);
        }


    }

    // 根据id查询商品
    @Override
    public Goods findById(Long id) {
        return goodsMapper.findById(id);
    }

    // 分页查询
    @Override
    public Page<Goods> search(Goods goods, int page, int size) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        if (goods != null && !StringUtil.isNullOrEmpty(goods.getGoodsName())) {
            queryWrapper.like("goodsName", goods.getGoodsName());
        }
        return goodsMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public List<GoodsDesc> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public GoodsDesc findDesc(Long id) {
        return goodsMapper.findDesc(id);
    }
}
